﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="IP地址" prop="IPAddress">
          <a-input v-model="entity.IPAddress" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="状态，是否禁用" prop="Status">
          <a-radio-group :default-value="entity.Status" v-model="entity.Status">
            <a-radio-button value="1"> 正常 </a-radio-button>
            <a-radio-button value="0"> 禁用 </a-radio-button>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="IP类型" prop="Type">
          <a-radio-group :default-value="entity.Type" v-model="entity.Type">
            <a-radio-button value="0"> 黑名单 </a-radio-button>
            <a-radio-button value="1"> 白名单 </a-radio-button>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object,
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 },
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()

      if (id) {
        this.loading = true
        this.$http.post('/Base_IPManager/ipmanager/GetTheData', { id: id }).then((resJson) => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_IPManager/ipmanager/SaveData', this.entity).then((resJson) => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
  },
}
</script>
